﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Navigation;
using DataFileApplication_WPF.Common;
using DataFileApplication_WPF.Extensions;

namespace DataFileApplication_WPF.Models
{
    [Serializable]
    public class DataGridItemStruct : NotifyPropertyChangedBase
    {

        // 新增：保存原始Key
        private string _originalKey;
        public string OriginalKey
        {
            get => _originalKey;
            set
            {
                if (_originalKey != value)
                {
                    _originalKey = value;
                    RaisePropertyChanged();
                }
            }
        }

        //新增：重命名状态
        private bool _renameState;
        public bool RenameState
        {
            get => _renameState;
            set
            {
                if (_renameState != value)
                {
                    _renameState = value;
                    RaisePropertyChanged();
                }
            }
        }

        //新增： 是否可以触发Key状态改变事件
        private bool _canRaiseKeyState;
        public bool CanRaiseKeyState
        {
            get => _canRaiseKeyState;
            set
            {
                if (_canRaiseKeyState != value)
                {
                    _canRaiseKeyState = value;
                }
            }
        }


        private string _key;
        public string Key
        {
            get { return _key; }
            set
            {
                if (_key != value)
                {
                    _key = value;
                    if (CanRaiseKeyState)
                    {
                        RaisePropertyChanged();
                    }
                }
            }
        }

        private object _value;
        public object Value
        {
            get { return _value; }
            set
            {
                if (_value != value)
                {
                    //值有效性验证  
                    switch (Type)
                    {
                        //case EnumDataType.Float:
                        //    IsValidValue = float.TryParse(value?.ToString(), out float floatValue);
                        //    break;
                        case EnumDataType.Integer:
                            IsValidValue = int.TryParse(value?.ToString(), out int intValue);
                            break;
                        case EnumDataType.String:
                            IsValidValue = !string.IsNullOrWhiteSpace(value?.ToString());
                            break;
                        case EnumDataType.Double:
                            IsValidValue = double.TryParse(value?.ToString(), out double doubleValue);
                            break;
                        case EnumDataType.Boolean:
                            IsValidValue = bool.TryParse(value?.ToString(), out bool boolValue);
                            break;
                    }


                    _value = value;
                    RaisePropertyChanged();

                }
            }
        }

        private EnumDataType _type;
        public EnumDataType Type
        {
            get { return _type; }
            set
            {
               if (_type != value)
                {
                    _type = value;
                   
                }
                RaisePropertyChanged();
            }
        }

        public int HeaderIndex { get; set; }

        //如果是数组元素，其索引值
        public int ArrayIndex { get; set; }

        private ObservableCollection<object> _valueList;
        //存储 值下拉菜单元素
        public ObservableCollection<object> ValueList
        {
            get { return _valueList; }
            set
            {
                _valueList = value;
                RaisePropertyChanged();
            }
        }

        //存储 类型下拉菜单元素
        private ObservableCollection<EnumDataType> _typeList;
        public ObservableCollection<EnumDataType> TypeList
        {
            get { return _typeList; }
            set
            {
                _typeList = value;
                RaisePropertyChanged();
            }
        }

        public DataGridComment DataGridComments { get; set; }

        // 新增  值有效性验证是否通过
        private bool _isValidValue;
        public bool IsValidValue
        {
            get { return _isValidValue; }
            set
            {
                _isValidValue = value;
                RaisePropertyChanged();

            }
        }






        public DataGridItemStruct()
        {
            TypeList = new ObservableCollection<EnumDataType>();
            ValueList = new ObservableCollection<object>();
            DataGridComments = new DataGridComment();

            ValueList.CollectionChanged += (s, e) =>
            {
                RaisePropertyChanged();
            };
            TypeList.CollectionChanged += (s, e) =>
            {
                RaisePropertyChanged();
            };
            //默认值 -1，不是数组元素
            ArrayIndex = -1;
        }

        public DataGridItemStruct Clone()
        {
            // 创建一个新的 DataGridItemStruct 对象
            var cloned = new DataGridItemStruct
            {
                Key = this.Key,
                Value = this.Value,
                Type = this.Type,
                HeaderIndex = this.HeaderIndex,
                ArrayIndex = this.ArrayIndex,
                ValueList = new ObservableCollection<object>(this.ValueList),
                TypeList = new ObservableCollection<EnumDataType>(this.TypeList),
                OriginalKey = this.OriginalKey,
                RenameState = this.RenameState,
                CanRaiseKeyState = this.CanRaiseKeyState
            };

            // 递归克隆 Comment 属性
            if (this.DataGridComments != null)
            {
                cloned.DataGridComments = this.DataGridComments.Clone();
            }

            return cloned;
        }
    }
}
